package com.joplus.housekeeper.utils;

import com.hyphenate.chatuidemo.DemoApplication;
import com.joplus.housekeeper.bean.MessageRecordListBean;
import com.joplus.housekeeper.bean.RecordListBean;
import com.joplus.housekeeper.bean.XmppMessage;
import com.joplus.housekeeper.bean.XmppUser;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmConfiguration;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.RealmResults;

/**
 * Created by zisheng on 2017/9/5.
 */

public class RealmHelper {

    private static final String DB_NAME = "joservice.realm";

    private Realm mRealm;
    public static RealmHelper instance;

    private RealmHelper() {
        Realm.init(DemoApplication.getInstance());
        mRealm = Realm.getInstance(new RealmConfiguration.Builder()
                .deleteRealmIfMigrationNeeded()
                .name(DB_NAME)
                .build());
    }

    public static RealmHelper instance() {
        if (instance == null) {
            synchronized (RealmHelper.class) {
                instance = new RealmHelper();
            }
        }
        return instance;
    }

    public void beginTran() {
        mRealm.beginTransaction();
    }

    public void insertNewUser(XmppUser xmppUser) {
        //    mRealm.beginTransaction();
        mRealm.copyToRealmOrUpdate(xmppUser);
        mRealm.commitTransaction();
    }

    public XmppUser createUser(String jid) {
        XmppUser object = mRealm.createObject(XmppUser.class, jid);
        object.setUserName(jid);
        return object;
    }

    public XmppMessage createMessage() {
        XmppMessage object = mRealm.createObject(XmppMessage.class);

        return object;
    }

    public void insertNewMessage(XmppUser xmppUser, XmppMessage xmppMessage) {
        //  mRealm.beginTransaction();
        if (xmppUser.getXmppMessages() == null) {
            RealmList<XmppMessage> xmppMessages = new RealmList<>();
            xmppUser.setXmppMessages(xmppMessages);
        }
        xmppUser.getXmppMessages().add(xmppMessage);
        mRealm.copyToRealmOrUpdate(xmppUser);
        mRealm.commitTransaction();

    }

    public void insertNewMessage(MessageRecordListBean xmppMessage) {
        mRealm.beginTransaction();
        mRealm.copyToRealmOrUpdate(xmppMessage);
        mRealm.commitTransaction();

    }

    public void removeUser(Class<? extends RealmObject> user) {
        mRealm.delete(user);
        mRealm.commitTransaction();
    }

    public RealmResults<XmppUser> allUser() {
        RealmResults<XmppUser> all = mRealm.where(XmppUser.class).findAll();
        return all;
    }

    public RealmResults<XmppUser> findUser(String jid) {
        RealmResults<XmppUser> userName = mRealm.where(XmppUser.class).equalTo("userName", jid).findAll();
        return userName;
    }

    public RealmResults<XmppMessage> findMessage(String messageId) {
        RealmResults<XmppMessage> messages = mRealm.where(XmppMessage.class).equalTo("messageId", messageId).findAll();
        return messages;
    }


    public RealmResults<XmppMessage> allConversation(String jid) {
        RealmResults<XmppMessage> allSorted = mRealm.where(XmppMessage.class).equalTo("from", jid).findAllSorted("messageTime");
        return allSorted;
    }

    public RealmResults<XmppMessage> newConversation(String jid, Long messageTime) {

        RealmResults<XmppMessage> allSorted = mRealm.where(XmppMessage.class).equalTo("from", jid).greaterThan("messageTime", messageTime).findAllSorted("messageTime");

        return allSorted;
    }


    public void addRealmChange(RealmChangeListener realmChangeListener) {
        mRealm.addChangeListener(realmChangeListener);
    }

    public RealmResults<MessageRecordListBean> findMessageList(String from) {
        RealmResults<MessageRecordListBean> allSorted = mRealm.where(MessageRecordListBean.class).contains("accountId", from).findAllSorted("messageDatetime");

        return allSorted;
    }
}
